package com.ccl.iot.template;import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.ccl.iot.publics.Res;
import com.ccl.iot.publics.Window;

import java.util.ArrayList;


public abstract class ActivityDialog extends StandardActivity {
	public static final String KEXTRA_NAME_CANCELABLE = "Cancelable";
	public static final String KEXTRA_NAME_CLOSE_ON_TOUCH_OUTSIDE = "CloseOnTouchOutside";
	public static final String KEXTRA_NAME_INTERFACE = "Interface";
	
	public interface DialogInterface{
		 void onShow(ActivityDialog aDialog);
		 void onCancel(ActivityDialog aDialog);
		 void onDismiss(ActivityDialog aDialog);
	}
	
	private static ArrayList<DialogInterface> gInterface = new ArrayList<DialogInterface>();
	
	public static void Show(Context aContext, Class<?> aClass, Bundle aBundle, DialogInterface aInterface){
		if (aInterface != null){
			synchronized(gInterface){
				if (gInterface.add(aInterface)){
					int tIndex;
					
					tIndex = gInterface.size() - 1;
					
					if (aBundle == null){
						aBundle = ObtainBundle();
					}
					
					aBundle.putInt(KEXTRA_NAME_INTERFACE, tIndex);
				}
			}
		}
		
		if (aBundle != null){
			Window.ShowActivity(aContext, aClass, Intent.FLAG_ACTIVITY_NEW_TASK, aBundle);
		}else{
			Window.ShowActivity(aContext, aClass, Intent.FLAG_ACTIVITY_NEW_TASK, new Object[0]);
		}
	}
	
	public static Bundle ObtainBundle(){
		return new Bundle();
	}
	
	public static Bundle ObtainBundle(boolean aCancelable, boolean aCloseOnTouchOutside){
		Bundle tBundle = ObtainBundle();
		
		tBundle.putBoolean(KEXTRA_NAME_CANCELABLE, aCancelable);
		tBundle.putBoolean(KEXTRA_NAME_CLOSE_ON_TOUCH_OUTSIDE, aCloseOnTouchOutside);
		
		return tBundle;
	}
	
	public static String GetString(Context aContext, Object aString){
		if (aString != null){
			if (aString instanceof Integer){
				return Res.GetString(aContext, (Integer) aString);
			}else if (aString instanceof String){
				return (String) aString;
			}else{
				return aString.toString();
			}
		}
		
		return null;
	}
	
	
	private boolean iCancelable = false;
	private boolean iCloseOnTouchOutside = false;
	
	private DialogInterface iDialogInterface = null;
	
	private LinearLayout iLayout = null;
	
	private boolean iShowing = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
		
		super.onCreate(savedInstanceState);
		
		initialize();
		
		initializeLayout();
	}
	
	private void initialize(){
		int tInterface = this.getIntent().getIntExtra(KEXTRA_NAME_INTERFACE, -1);
		
		iCancelable = this.getIntent().getBooleanExtra(KEXTRA_NAME_CANCELABLE, iCancelable);
		iCloseOnTouchOutside = this.getIntent().getBooleanExtra(KEXTRA_NAME_CLOSE_ON_TOUCH_OUTSIDE, iCloseOnTouchOutside);
	
		if (tInterface >= 0){
			synchronized(gInterface){
				if (gInterface.size() > tInterface){
					iDialogInterface = gInterface.get(tInterface);
				}
			}
		}
	}
	
	private void initializeLayout(){
		iLayout = new LinearLayout(this);
		iLayout.setBackgroundColor(Color.TRANSPARENT);
		
		super.setContentView(iLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
	}
	
	public String getString(String aName){
		return this.getIntent().getStringExtra(aName);
	}
	
	public DialogInterface getDialogInterface(){
		return iDialogInterface;
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		if (view != null && iLayout != null){
			if (iLayout.getChildCount() > 0){
				iLayout.removeAllViews();
			}
			
			if (params != null){
				iLayout.addView(view, params);
			}else{
				iLayout.addView(view);
			}
		}
	}
	
	@Override
	public void setContentView(View view) {
		setContentView(view, null);
	}

	@Override
	public void setContentView(int layoutResID) {
		if (iLayout != null){
			LayoutInflater.from(this).inflate(layoutResID, iLayout);
		}
	}
	
	public boolean isShowing(){
		return iShowing;
	}
	
	public void dismiss(){
		this.finish();
	}
	
	public void cancel(){
		if (iDialogInterface != null){
			iDialogInterface.onCancel(this);
		}
		
		dismiss();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (iCloseOnTouchOutside){
			float tX = event.getX();
			float tY = event.getY();
			
			if (iLayout == null || tX < 0 || tY < 0 || tX > iLayout.getWidth() || tY > iLayout.getHeight()){
				cancel();
				
				return true;
			}
		}
		
		return false;
	}
	
	@Override
	public void onBackPressed() {
		if (iCancelable){
			cancel();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		iShowing = true;
		
		if (iDialogInterface != null){
			iDialogInterface.onShow(this);
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		
		iShowing = false;
	}
	
	@Override
	protected void onDestroy() {
		if (iDialogInterface != null){
			iDialogInterface.onDismiss(this);
			
			synchronized(gInterface){
				gInterface.remove(iDialogInterface);
			}
		}

		super.onDestroy();
	}
}
